package vfs.common.command.remote;

import vfs.common.Locale;
import vfs.common.command.AbstractCommand;
import vfs.common.file.VirtualFileAlreadyExistsException;
import vfs.common.file.VirtualFileNotFoundException;

import java.io.IOException;

/**
 * User: serge
 * Date: 22.11.13
 * Time: 5:43
 */
public class MakeFileCommand extends AbstractCommand implements RemoteCommand
{
    private String path = "";

    @Override
    public void execute()
    {
        if (params.length != 2)
        {
            tty.broadcast(Locale.CMD_EXC_WRONG_PARAMS, user);
        }

        path = params[1];

        try
        {
            filesystem.makeFile(path, false, user);
            filesystem.serialize();
            tty.send(Locale.CMD_CREATED_OK, user);
            tty.broadcast(String.format("%s performs command: MF %s",user.getName(),path), user);
        }
        catch (VirtualFileAlreadyExistsException e)
        {
            //            e.printStackTrace();
            tty.send("Such file already exists", user);
        }
        catch (VirtualFileNotFoundException e)
        {
            //            e.printStackTrace();
            tty.send(Locale.CMD_EXC_PATH_NOT_FOUND, user);
        }
        catch (IOException e)
        {
            e.printStackTrace();  //Todo generated catch
        }
    }
}
